package cn.xspace.wnx.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.squareup.picasso.Picasso;
import com.xspace.android.xafcommon.util.LogUtil;
import com.xspace.android.xafcommon.view.DragImageView;

import java.io.File;

import com.xspace.android.xafcommon.base.BaseFragment;
import cn.xspace.wnx.R;
import cn.xspace.chatui.bean.MsgRecordEntity;

/**
 * Created by Administrator on 2016/11/7.
 */

public class ImageLargeFragment extends BaseFragment {
    DragImageView mDragImageView = null;
    public static String SELECT_BEAN = "selectBean";
    int window_width;
    int window_height;

    public static ImageLargeFragment newInstance(MsgRecordEntity user) {

        Bundle args = new Bundle();
        args.putSerializable(SELECT_BEAN, user);

        ImageLargeFragment fragment = new ImageLargeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View initView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_image_large, null);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mDragImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        mDragImageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return false;
            }
        });
    }

    @Override
    protected void initFindViewById(View view) {
        mDragImageView = (DragImageView) view.findViewById(R.id.dragimage);
    }

    @Override
    public void initData() {
        MsgRecordEntity bean = (MsgRecordEntity) fragmentArgs.get(SELECT_BEAN);
/** 获取可見区域高度 **/
        WindowManager manager = getActivity().getWindowManager();
        window_width = manager.getDefaultDisplay().getWidth();
        window_height = manager.getDefaultDisplay().getHeight();

//        mDragImageView.setTag();
        mDragImageView.setScreen_H(window_height);
        mDragImageView.setScreen_W(window_width);
        mDragImageView.setmActivity(getActivity(), true);

        if (new File(bean.mFileEntity.file_url_local).exists()) {
            LogUtil.d(bean.mFileEntity.file_url_local);
            Picasso.with(getActivity())
                    .load(new File(bean.mFileEntity.file_url_local))
                    .placeholder(R.drawable.chat_default_image)
                    .error(R.drawable.chat_default_image_error)
                    .into(mDragImageView);
        } else if (!TextUtils.isEmpty(bean.mFileEntity.file_url_remote)) {
            LogUtil.d(bean.mFileEntity.file_url_remote);
            Picasso.with(getActivity())
                    .load(bean.mFileEntity.file_url_remote)
                    .placeholder(R.drawable.chat_default_image)
                    .error(R.drawable.chat_default_image_error)
                    .into(mDragImageView);
        } else {
            mDragImageView.setImageDrawable(getResources().getDrawable(R.drawable.chat_default_image_error));
        }
    }
}
